package com.ruoyi.jjm.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.jjm.domain.CountyStatistics;
import com.ruoyi.jjm.mapper.ClientUserOrderMapper;
import com.ruoyi.jjm.mapper.VillageMapper;
import com.ruoyi.jjm.vo.VillageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jjm.mapper.TownStatisticsMapper;
import com.ruoyi.jjm.domain.TownStatistics;
import com.ruoyi.jjm.service.ITownStatisticsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 乡统计信息Service业务层处理
 * 
 * @author tsr
 * @date 2020-07-26
 */
@Service
public class TownStatisticsServiceImpl implements ITownStatisticsService 
{
    @Autowired
    private TownStatisticsMapper townStatisticsMapper;

    @Autowired
    ClientUserOrderMapper clientUserOrderMapper;

    @Autowired
    VillageMapper villageMapper;

    /**
     * 查询乡统计信息
     * 
     * @param id 乡统计信息ID
     * @return 乡统计信息
     */
    @Override
    public TownStatistics selectTownStatisticsById(Long id)
    {
        return townStatisticsMapper.selectTownStatisticsById(id);
    }

    /**
     * 查询乡统计信息列表
     * 
     * @param townStatistics 乡统计信息
     * @return 乡统计信息
     */
    @Override
    public List<TownStatistics> selectTownStatisticsList(TownStatistics townStatistics)
    {
        return townStatisticsMapper.selectTownStatisticsList(townStatistics);
    }

    /**
     * 新增乡统计信息
     * 
     * @param townStatistics 乡统计信息
     * @return 结果
     */
    @Override
    public int insertTownStatistics(TownStatistics townStatistics)
    {
        townStatistics.setCreateTime(DateUtils.getNowDate());
        return townStatisticsMapper.insertTownStatistics(townStatistics);
    }

    /**
     * 修改乡统计信息
     * 
     * @param townStatistics 乡统计信息
     * @return 结果
     */
    @Override
    public int updateTownStatistics(TownStatistics townStatistics)
    {
        townStatistics.setUpdateTime(DateUtils.getNowDate());
        return townStatisticsMapper.updateTownStatistics(townStatistics);
    }

    /**
     * 删除乡统计信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTownStatisticsByIds(String ids)
    {
        return townStatisticsMapper.deleteTownStatisticsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除乡统计信息信息
     * 
     * @param id 乡统计信息ID
     * @return 结果
     */
    @Override
    public int deleteTownStatisticsById(Long id)
    {
        return townStatisticsMapper.deleteTownStatisticsById(id);
    }

    @Override
    public TownStatistics selectTownStatisticsSum(Long xianId) {
        return townStatisticsMapper.selectTownStatisticsSum(xianId);
    }

    @Override
    public void insertOrUpdateTownStatistics(TownStatistics townStatistics) {
        TownStatistics record =
                townStatisticsMapper.selectTownStatisticsByTownIdAndCountyId(townStatistics);

        townStatistics.setTotalBuyNum(clientUserOrderMapper.countXiangTotal(townStatistics.getTownId()));
        //这个不需要了，不用从那个village里面找，直接统计clientUser
//        VillageInfo villageInfo = villageMapper.selectVillageInfoGroupByXiang(townStatistics.getTownId());
//        townStatistics.setAccurateNum(villageInfo.getPopulation());
//        townStatistics.setHouseholdsNum(villageInfo.getResidents());
//        townStatistics.setCleanCoalNum(villageInfo.getJjm());
//        townStatistics.setNotCleanCoalNum(townStatistics.getAccurateNum() - townStatistics.getCleanCoalNum());
        if(record == null){
            townStatistics.setCreateBy(townStatistics.getUpdateBy());
            townStatistics.setCreateTime(DateUtils.getNowDate());
            townStatisticsMapper.insertTownStatistics(townStatistics);
        } else {
            townStatistics.setId(record.getId());
            townStatistics.setUpdateTime(DateUtils.getNowDate());
            townStatisticsMapper.updateTownStatistics(townStatistics);
        }

    }

    @Override
    public CountyStatistics selectSumStatisticsByXianId(Long xianId) {
        return townStatisticsMapper.selectSumStatisticsByXianId(xianId);
    }

    @Override
    public Double sumBuyNumByCountyId(Long countyId) {
        return townStatisticsMapper.sumBuyNumByCountyId(countyId);
    }
}
